package com.ruoyi.controller;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.domain.model.XcxLoginBody;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.XcxLoginService;
import com.ruoyi.service.*;
import com.ruoyi.uitity.MinioUtil;
import com.ruoyi.uitity.WxMessageProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.springframework.web.multipart.MultipartFile;



@RestController
public class AiController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private XcxLoginService xcxLoginService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private WxMessageProducer producer;
    @Autowired
    private IT06Service t06Service;
    @Autowired
    private IT07Service t07Service;

    @Autowired
    private AIService aiService;

    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private IT10Service t10Service;

    @Autowired
    private IT12Service t12Service;

    @PostMapping("/login1")
    @Log(title = "登录日志", businessType = BusinessType.LOGIN)
    @ApiOperation("小程序登录")
    public AjaxResult login(@RequestBody XcxLoginBody loginUser, HttpServletRequest request) {
        if (StringUtils.isEmpty(loginUser.getUsername()) || StringUtils.isEmpty(loginUser.getPassword())) {
            return AjaxResult.error(HttpStatus.ERROR, "用户名或密码不能为空");
        }
        AjaxResult ajax = AjaxResult.success();
        String token =xcxLoginService.login(loginUser.getUsername(), loginUser.getPassword());
        System.out.println("token"+token);
        ajax.put(Constants.TOKEN, token);
        T06 t06 = t06Service.selectByusername(loginUser.getUsername());
        redisTemplate.opsForValue().set("loginuser:"+t06.getF0601(),t06, 30, TimeUnit.MINUTES);
        return ajax;
    }
    @ApiOperation("创建会话")
    @Log(title = "会话日志", businessType = BusinessType.RESERCH)
    @PostMapping("/ask/ans")
    public AjaxResult wenda(@RequestBody Askbody askbody, HttpServletRequest request) throws IOException {
        return aiService.wenda(askbody.getContent(),request);
    }

    @ApiOperation("注册")
    @PostMapping("/register1")
//    @Log(title = "注册日志", businessType = BusinessType.RESERCH)
    public AjaxResult register(@RequestBody RegisterBody registorBody) {
        String msg=aiService.register(registorBody);
        if (msg.isEmpty()){
            return AjaxResult.success();
        }
        return new AjaxResult(HttpStatus.BAD_REQUEST,msg);
    }
    @ApiOperation("获取用户信息")
    @GetMapping("/wx/userinfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        T06 t06 = (T06) redisTemplate.opsForValue().get("loginuser:"+userId);
        if(t06!=null){
            return AjaxResult.success(t06);
        }
        return new AjaxResult(500, "用户信息获取失败");
    }
    @ApiOperation("今日数据")
    @GetMapping("/wx/todaytoken")
    public AjaxResult getToday(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        return AjaxResult.success(t07Service.counttoken(userId));
    }
    @PostMapping(value = "/wx/uploadimage")
    public AjaxResult uploadReport(MultipartFile file,HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        String filename = UUID.randomUUID().toString();
        boolean flag=aiService.updateUserImg(file,userId,filename);
        if (flag)
        {
            redisTemplate.opsForValue().set("loginuser:"+userId,t06Service.selectT06ByF0601(userId), 30, TimeUnit.MINUTES);
            return AjaxResult.success(filename);
        }
        return new AjaxResult(HttpStatus.ERROR,"上传失败");
    }
    @PostMapping("/wx/preview")
    public AjaxResult preview(@RequestBody String fileName) {
        return AjaxResult.success(minioUtil.getFileUrl(fileName));
    }
    @PostMapping("/wx/update")
    public AjaxResult update(@RequestBody T06 t06,HttpServletRequest request) {
        int s=t06Service.updateT06(t06);
        if (s==0)
        {
            return AjaxResult.warn("修改失败");
        }
        redisTemplate.opsForValue().set("loginuser:"+t06.getF0601(),t06Service.selectT06ByF0601(t06.getF0601()), 30, TimeUnit.MINUTES);
        return AjaxResult.success("修改成功");
    }
    @GetMapping("/wx/czrecord")
    public AjaxResult getCzrecord(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        T10 t10=new T10();
        t10.setF0601(userId);
        List<T10> list =t10Service.selectT10List(t10);
        return AjaxResult.success(list);
    }
    @GetMapping("/wx/QueryTrends")
    public AjaxResult QueryTrends(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        List<QUTH> list = aiService.QueryTrends(userId);
        return AjaxResult.success(list);
    }

    @GetMapping("/wx/getsele")
    public AjaxResult getPayRecords(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        List<T07> list = t07Service.selectlist(userId);
        return AjaxResult.success(list);
    }
    @GetMapping("/wx/getCom")
    public AjaxResult getT03List(HttpServletRequest request) {
        List<T03> list = aiService.getT03List();
        return AjaxResult.success(list);
    }
    @GetMapping("/wx/getDept/{ComId}")
    public AjaxResult getDept(@PathVariable("ComId") Long ComId) {
        List<T05> list = aiService.getT05List(ComId);
        return AjaxResult.success(list);
    }
    @PostMapping("/wx/updateById")
    public AjaxResult updateById(@RequestBody T06small t06small,HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        int s=aiService.updateById(t06small,userId);
        if (s==0)
        {
            return AjaxResult.warn("修改失败");
        }
        T06 t06 =t06Service.selectT06ByF0601(userId);
        redisTemplate.opsForValue().set("loginuser:"+t06.getF0601(),t06Service.selectT06ByF0601(t06.getF0601()), 30, TimeUnit.MINUTES);
        return AjaxResult.success("修改成功");
    }
    @PostMapping("/wx/cz")
    public AjaxResult cz(@RequestBody Long f1002,HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        T06 t06 = t06Service.selectT06ByF0601(userId);
        Long tatel=(Long) t06.getF0611()+f1002*208;
        t06.setF0611(tatel);
        t06Service.updateT06(t06);
        T10 t10=new T10();
        t10.setF0301(t06.getF0301());
        t10.setF0601(t06.getF0601());
        t10.setF1002(f1002);
        Date date = new Date();
        t10.setF1003(date);
        t10.setF1005(tatel);
        t10Service.insertT10(t10);
        redisTemplate.opsForValue().set("loginuser:"+t06.getF0601(),t06Service.selectT06ByF0601(t06.getF0601()), 30, TimeUnit.MINUTES);
        return AjaxResult.success("充值成功");
    }
}
